package com.hww.base.util;

import android.annotation.SuppressLint;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

public class DateUtil {

	public static final String FORMAT_DATE_EN = "yyyy-MM-dd";
	public static final String FORMAT_DATE_CN = "yyyy年MM月dd日";

	public static final String FORMAT_TIME_CN = "yyyy年MM月dd HH时mm分ss秒";
	public static final String FORMAT_TIME_CN_2 = "yyyy年MM月dd HH时mm分";
	public static final String FORMAT_TIME_EN = "yyyy-MM-dd HH:mm:ss";
	public static final String FORMAT_TIME_EN_SSSS = "yyyy-MM-dd HH:mm:ss SSSS";
	public static final String FORMAT_TIME_EN_2 = "yyyy-MM-dd HH:mm";

	public static final String FORMAT_DAY_CN = "HH时mm分ss秒";
	public static final String FORMAT_DAY_CN_2 = "HH时mm分";
	public static final String FORMAT_DAY_EN = "HH:mm:ss";
	public static final String FORMAT_DAY_EN_2 = "HH:mm";
	public static final String FORMAT_DAY_EN_3 = "mm:ss";

	/**
	 * 把日期字符串格式化成日期类型
	 *
	 * @param dateStr
	 * @param format
	 * @return
	 */
	public static Date convert2Date(String dateStr, String format) {
		@SuppressLint("SimpleDateFormat")
		SimpleDateFormat simple = new SimpleDateFormat(format);
		try {
			simple.setLenient(false);
			return simple.parse(dateStr);
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 把日期类型格式化成字符串
	 *
	 * @param date
	 * @param format
	 * @return
	 */
	public static String convert2String(Date date, String format) {
		SimpleDateFormat formater = new SimpleDateFormat(format);
		try {
			return formater.format(date);
		} catch (Exception e) {
			return null;
		}
	}

	public static String currentString() {
		SimpleDateFormat formater = new SimpleDateFormat(FORMAT_TIME_EN);
		try {
			return formater.format(new Date());
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 转sql的time格式
	 *
	 * @param date
	 * @return
	 */
	public static java.sql.Timestamp convertSqlTime(Date date) {
		java.sql.Timestamp timestamp = new java.sql.Timestamp(date.getTime());
		return timestamp;
	}

	/**
	 * 转sql的日期格式
	 *
	 * @param date
	 * @return
	 */
	public static java.sql.Date convertSqlDate(Date date) {
		java.sql.Date Datetamp = new java.sql.Date(date.getTime());
		return Datetamp;
	}

	/**
	 * 获取当前日期
	 *
	 * @param format
	 * @return
	 */
	public static String getCurrentDate(String format) {
		return new SimpleDateFormat(format).format(new Date());
	}

	/**
	 * 获取时间戳
	 *
	 * @return
	 */
	public static long getTimestamp() {
		return System.currentTimeMillis();
	}

	/**
	 * 获取月份的天数
	 *
	 * @param year
	 * @param month
	 * @return
	 */
	public static int getDaysOfMonth(int year, int month) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month - 1, 1);
		return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 获取日期的年
	 *
	 * @param date
	 * @return
	 */
	public static int getYear(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.YEAR);
	}

	/**
	 * 获取日期的月
	 *
	 * @param date
	 * @return
	 */
	public static int getMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.MONTH) + 1;
	}

	/**
	 * 获取日期的日
	 *
	 * @param date
	 * @return
	 */
	public static int getDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.DATE);
	}

	/**
	 * 获取日期的时
	 *
	 * @param date
	 * @return
	 */
	public static int getHour(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.HOUR);
	}

	/**
	 * 获取日期的分种
	 *
	 * @param date
	 * @return
	 */
	public static int getMinute(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.MINUTE);
	}

	/**
	 * 获取日期的秒
	 *
	 * @param date
	 * @return
	 */
	public static int getSecond(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.SECOND);
	}

	/**
	 * 获取星期几
	 *
	 * @param date
	 * @return
	 */
	public static int getWeekDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
		return dayOfWeek - 1;
	}

	/**
	 * 获取哪一年共有多少周
	 *
	 * @param year
	 * @return
	 */
	public static int getMaxWeekNumOfYear(int year) {
		Calendar c = new GregorianCalendar();
		c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);
		return getWeekNumOfYear(c.getTime());
	}

	/**
	 * 取得某天是一年中的多少周
	 *
	 * @param date
	 * @return
	 */
	public static int getWeekNumOfYear(Date date) {
		Calendar c = new GregorianCalendar();
		c.setFirstDayOfWeek(Calendar.MONDAY);
		c.setMinimalDaysInFirstWeek(7);
		c.setTime(date);
		return c.get(Calendar.WEEK_OF_YEAR);
	}

	/**
	 * 取得某天所在周的第一天
	 *
	 * @param date
	 * @return
	 */
	public static Date getFirstDayOfWeek(Date date) {
		Calendar c = new GregorianCalendar();
		c.setFirstDayOfWeek(Calendar.MONDAY);
		c.setTime(date);
		c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
		return c.getTime();
	}

	/**
	 * 取得某天所在周的最后一天
	 *
	 * @param date
	 * @return
	 */
	public static Date getLastDayOfWeek(Date date) {
		Calendar c = new GregorianCalendar();
		c.setFirstDayOfWeek(Calendar.MONDAY);
		c.setTime(date);
		c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6);
		return c.getTime();
	}

	/**
	 * 取得某年某周的第一天 对于交叉:2008-12-29到2009-01-04属于2008年的最后一周,2009-01-05为2009年第一周的第一天
	 *
	 * @param year
	 * @param week
	 * @return
	 */
	public static Date getFirstDayOfWeek(int year, int week) {
		Calendar calFirst = Calendar.getInstance();
		calFirst.set(year, 0, 7);
		Date firstDate = getFirstDayOfWeek(calFirst.getTime());

		Calendar firstDateCal = Calendar.getInstance();
		firstDateCal.setTime(firstDate);

		Calendar c = new GregorianCalendar();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, Calendar.JANUARY);
		c.set(Calendar.DATE, firstDateCal.get(Calendar.DATE));

		Calendar cal = (GregorianCalendar) c.clone();
		cal.add(Calendar.DATE, (week - 1) * 7);
		firstDate = getFirstDayOfWeek(cal.getTime());

		return firstDate;
	}

	/**
	 * 取得某年某周的最后一天 对于交叉:2008-12-29到2009-01-04属于2008年的最后一周, 2009-01-04为
	 * 2008年最后一周的最后一天
	 *
	 * @param year
	 * @param week
	 * @return
	 */
	public static Date getLastDayOfWeek(int year, int week) {
		Calendar calLast = Calendar.getInstance();
		calLast.set(year, 0, 7);
		Date firstDate = getLastDayOfWeek(calLast.getTime());

		Calendar firstDateCal = Calendar.getInstance();
		firstDateCal.setTime(firstDate);

		Calendar c = new GregorianCalendar();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, Calendar.JANUARY);
		c.set(Calendar.DATE, firstDateCal.get(Calendar.DATE));

		Calendar cal = (GregorianCalendar) c.clone();
		cal.add(Calendar.DATE, (week - 1) * 7);
		Date lastDate = getLastDayOfWeek(cal.getTime());

		return lastDate;
	}

	/**
	 * long型时间转换
	 *
	 * @param longTime 长整型时间
	 * @return 2013年7月3日 18:05(星期三)
	 */
	public static String convertDayOfWeek(String timeFormat, long longTime) {
		// 日历实例
		Calendar c = Calendar.getInstance();
		c.setTime(new Date(longTime));

		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH);
		int date = c.get(Calendar.DATE);
		int hour = c.get(Calendar.HOUR_OF_DAY);
		String h = hour > 9 ? String.valueOf(hour) : "0" + hour;
		int minute = c.get(Calendar.MINUTE);
		String m = minute > 9 ? String.valueOf(minute) : "0" + minute;
		return String.format(Locale.getDefault(), timeFormat, year, month + 1, date, h, m,
				converToWeek(c.get(Calendar.DAY_OF_WEEK)));
	}

	/**
	 * 转换数字的星期为字符串的
	 *
	 * @param w 星期
	 * @return 星期x
	 */
	private static String converToWeek(int w) {
		String week = null;

		switch (w) {
		case 1:
			week = "星期日";
			break;
		case 2:
			week = "星期一";
			break;
		case 3:
			week = "星期二";
			break;
		case 4:
			week = "星期三";
			break;
		case 5:
			week = "星期四";
			break;
		case 6:
			week = "星期五";
			break;
		case 7:
			week = "星期六";
			break;
		default:
			break;
		}
		return week;
	}

	/**
	 * 在之前
	 */
	private static final int TIME_BEFORE = 1;
	/**
	 * 在中间
	 */
	private static final int TIME_ING = 2;
	/**
	 * 在之后
	 */
	private static final int TIME_AFTER = 3;

	/**
	 * 计算时间是否在区间内
	 *
	 * @param time  time
	 * @param time1 time
	 * @param time2 time
	 * @return {@link DateUtil#TIME_BEFORE}{@link DateUtil#TIME_ING}{@link DateUtil#TIME_AFTER}
	 */
	public static int betweenTime(long time, long time1, long time2) {
		// 时间1大
		if (time1 > time2) {
			long testTime = time1;
			time1 = time2;
			time2 = testTime;
		}

		if (time1 > time) {
			// 在之前
			return TIME_BEFORE;
		} else if (time2 < time) {
			// 在之后
			return TIME_AFTER;
		} else {
			// 在中间
			return TIME_ING;
		}
	}

	public static long getCurrentYMDLong() {
		Date date = new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		int day = calendar.get(Calendar.DATE);
		Date d = new Date(year - 1900, month, day, 0, 0, 0);
		return d.getTime();
	}

}